import tkinter as tk
from tkinter import messagebox

'''
1.在文本框中添加图片和按钮时，必须带参数比如image=“” ,window=“”
2.添加的图片必须为全局变量,添加的按钮必须为局部变量
3.使用bind绑定函数时，被绑定的函数必须有第二个参数event，使用['command']时不需要第二个参数
'''
class application(tk.Frame):
    def __init__(self,master=None):
        super().__init__()
        self.master=master
        self.creat_app()

    def creat_app(self):
        self.b01=tk.Button(self.master,text="插入文字",font=("arial",14))
        self.b01.grid(row=1,column=0)
        self.b01['command']=self.insertText

        self.b02=tk.Button(self.master,text='复制所有文字，并黏贴到最后',font=("arial",14))
        self.b02.grid(row=1,column=2)
        self.b02['command']=self.copyAlltext

        self.b03=tk.Button(self.master,text='插入图片',font=('arial',14))
        self.b03.grid(row=1,column=3)
        self.b03['command']=self.insertImage

        self.b04=tk.Button(self.master,text='插入按钮',font=('arial',14))
        self.b04.grid(row=1,column=4)
        self.b04['command']=self.addWidget

        self.b05=tk.Button(self.master,text='add_tag',font=('arial',14))
        self.b05.grid(row=1,column=5)
        self.b05['command']=self.addTag

        self.text01=tk.Text(self.master,width=100,height=10)
        self.text01.grid(row=2,column=2)

    def insertText(self):
        self.text01.insert(1.0,'https://www.baidu.com')

    def copyAlltext(self):
        text=self.text01.get(1.0,tk.END)
        self.text01.insert(tk.END,text)

    def insertImage(self):
        self.photo=tk.PhotoImage(file='img.png')
        self.text01.image_create(tk.END,image=self.photo)

    def addWidget(self):
        w01=tk.Button(self.text01,text='我是被添加进来的')
        self.text01.window_create(tk.INSERT,window=w01)

    def addTag(self):
        self.text01.tag_add('百度',1.0,1.21)
        self.text01.tag_config('百度',background='red',foreground='blue')
        self.text01.tag_bind('百度','<Button-1>',self.pt)

    def pt(self,event):
        self.insertText()

root=tk.Tk()
root.geometry('1200x400')
app=application(root)
root.mainloop()






